Crate svg2pdf

source ·
Expand description

Convert SVG files to PDFs.

This crate allows to convert static (i.e. non-interactive) SVG files to either standalone PDF files or Form XObjects that can be embedded in another PDF file and used just like images.

The conversion will translate the SVG content to PDF without rasterizing them, so no quality is lost.

Example

This example reads an SVG file and writes the corresponding PDF back to the disk.

let path = "tests/svg/custom/integration/matplotlib/time_series.svg";
let svg = std::fs::read_to_string(path)?;

// This can only fail if the SVG is malformed. This one is not.
let pdf = svg2pdf::convert_str(&svg, svg2pdf::Options::default())?;

// ... and now you have a Vec<u8> which you could write to a file or
// transmit over the network!
std::fs::write("target/time_series.pdf", pdf)?;

Supported features

In general, a large part of the SVG specification is supported, including features like:

  • Path drawing with fills and strokes
  • Gradients
  • Patterns
  • Clip paths
  • Masks
  • Transformation matrices
  • Respecting the keepAspectRatio attribute
  • Raster images and nested SVGs

Unsupported features

Among the unsupported features are currently:

  • The spreadMethod attribute of gradients
  • Filters
  • Raster images are not color managed but use PDF’s DeviceRGB color space
  • A number of features that were added in SVG2

Structs

  • Set size and scaling preferences for the conversion.

Functions